<?php

namespace app\airobot\controller;

use app\airobot\CommonLogic;
use app\airobot\model\BattleBox;
use app\airobot\model\OriginBattleConfig;
use app\airobot\model\OriginRobotSpinConfig;
use app\airobot\model\UserModel;
use think\admin\Controller;
use think\facade\Db;
use ZipArchive;

/**
 * 超管系统-Battle列表
 * Class Banner
 * @package app\airobot\controller
 */
class BattleList extends Controller
{
    private $table = 'battle';
    public $commonLogic;

    /**
     * Battle列表
     * @auth true
     */
    public function index()
    {
        $this->commonLogic = new CommonLogic();
        $this->title = 'Battle列表';
        $query = $this->_query($this->table);
        $where = [];
        if (!empty($_GET['status'])) {
            $where['status'] = $_GET['status'];
        }
        $query->where($where);
        if (!empty($_GET['have_true_user'])) {
            if ($_GET['have_true_user'] == 1) {
                $query->where('true_user_count', '>', 0);
            } else {
                $query->where('true_user_count', '=', 0);
            }
            $this->true_user_count = $_GET['have_true_user'];
        }
        if (!empty($_GET['user_id'])) {
            $query->whereOr([['user_id_1', '=', $_GET['user_id']], ['user_id_2', '=', $_GET['user_id']], ['user_id_3', '=', $_GET['user_id']], ['user_id_4', '=', $_GET['user_id']]]);
        }
        if (!empty($_GET['battle_id'])) {
            $where['id'] = $_GET['battle_id'];
        }

        $query->order('status asc,id desc')->page();
    }

    protected function _index_page_filter(array &$data)
    {
        $this->commonLogic = new CommonLogic();
        $battleIds = [];
        foreach ($data as $vo) {
            $battleIds[] = $vo['id'];
        }

        // 查询盒子信息
        $battleBoxModel = new BattleBox();
        $battleBoxList = $battleBoxModel->queryList([], ['field' => 'battle_id', 'data' => $battleIds], [], [], [], "", $field = "*", $order = "");
        $battleBoxArr = [];
        foreach ($battleBoxList as $vo) {
            $battleBoxArr[$vo['battle_id']][] = $vo["box_id"];
        }
        $battleBoxMap = [];
        foreach ($battleBoxArr as $key => $vo) {
            $battleBoxMap[$key] = implode("|", $vo);
        }

        // 查询用户
        /* $userIds = [];
         foreach ($data as $vo) {
             if ($vo["user_id_1"] != "" && $vo["user_id_1"] != "0") {
                 $userIds[] = $vo["user_id_1"];
             }
             if ($vo["user_id_2"] != "" && $vo["user_id_2"] != "0") {
                 $userIds[] = $vo["user_id_2"];
             }
             if ($vo["user_id_3"] != "" && $vo["user_id_3"] != "0") {
                 $userIds[] = $vo["user_id_3"];
             }
             if ($vo["user_id_4"] != "" && $vo["user_id_4"] != "0") {
                 $userIds[] = $vo["user_id_4"];
             }
         }
         $userModel = new UserModel();
         $userList = $userModel->queryList([], ['field' => 'id', 'data' => $userIds], [], [], [], "", $field = "*", $order = "");
         $userMap = array_column($userList, null, 'id');*/

        foreach ($data as &$vo) {
            $trueUserCount = 0;
            $userIds = [];
            if ($vo["user_id_1"] != "" && $vo["user_id_1"] != "0") {
                $uid = $vo["user_id_1"];
                $userIds[] = $uid;
                if (isset($userMap[$uid]) && $userMap[$uid]["user_type"] == 0) {
                    $trueUserCount++;
                }
            }
            if ($vo["user_id_2"] != "" && $vo["user_id_2"] != "0") {
                $uid = $vo["user_id_2"];
                $userIds[] = $uid;
                if (isset($userMap[$uid]) && $userMap[$uid]["user_type"] == 0) {
                    $trueUserCount++;
                }
            }
            if ($vo["user_id_3"] != "" && $vo["user_id_3"] != "0") {
                $uid = $vo["user_id_3"];
                $userIds[] = $uid;
                if (isset($userMap[$uid]) && $userMap[$uid]["user_type"] == 0) {
                    $trueUserCount++;
                }
            }
            if ($vo["user_id_4"] != "" && $vo["user_id_4"] != "0") {
                $uid = $vo["user_id_4"];
                $userIds[] = $uid;
                if (isset($userMap[$uid]) && $userMap[$uid]["user_type"] == 0) {
                    $trueUserCount++;
                }
            }
            $vo["user_ids"] = implode("|", $userIds);
            $vo["box_ids"] = $battleBoxMap[$vo["id"]];
            $vo["cost_amount"] = $this->commonLogic->DealAmount($vo['cost_amount']);
        }
    }
}